Skip to content

feat(schema): Enforce unique node names within a network - #751

Open
Batch21 wants to merge 1 commit into
mainfrom
unique-node-names
Open

feat(schema): Enforce unique node names within a network#751
Batch21 wants to merge 1 commit into
mainfrom
unique-node-names

Conversation

@Batch21

@Batch21 Batch21 commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

Because node names in pywr-core are based on both the name and sub-name attrs and because schema nodes can add multiple nodes to core it is possible for two schema nodes with the same name to be added to core without raising an error. This can result in a misleading error such as UnresolvedNode. This PR solves this by checking for duplicates in the schema, though there might be a better way of doing this.

@Batch21
Batch21 requested a review from jetuk August 16, 2026 19:22

@jetuk jetuk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes look good to me. I would just like to discuss whether we should add this to the core builder infrastructure in some fashion.

/// not fail on its own: it silently binds every reference to whichever entry appears first
/// and leaves the other unreachable.
///
/// `pywr-core` cannot catch this for us. Its duplicate check runs over the *expanded* core

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only question to ask ourselves is whether this should be caught in pywr-core during build. It would be possible to use a namespace builder (or something) to permit the construction of only one parent node name.

I guess is it more or less complex to do it there. Is the addition of a validation step going to confusing or difficult to maintain.

/// `pywr-core` cannot catch this for us. Its duplicate check runs over the *expanded* core
/// nodes, keyed by name **and** sub-name. One schema node may expand to several core nodes,
/// and composite types such as [`crate::nodes::DelayNode`] emit only sub-named ones.
pub fn validate(&self) -> Result<(), SchemaError> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might argue if we do this that we should separate the error types between validation and add_to_network ValidationError.

/// Every entry of [`NetworkSchema::nodes`] and [`NetworkSchema::virtual_nodes`] must have a
/// unique name.
///
/// Duplicates must be rejected here because name resolution in this crate is

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reads a bit like internal documentation, not documentation of the function itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants